package com.ai.util;

import com.ai.common.constant.enumP.CameraDetectStatus;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GetCameraPreviewURLUtil {

    public static String getCameraRtspUrl(String cameraIndexCode,Integer streamType){
        String urlJson = GetCameraPreviewURLUtil.GetCameraPreviewURL(cameraIndexCode,streamType);
        JSONObject jsonObject = JSONObject.parseObject(urlJson);
        JSONObject msg = (JSONObject) jsonObject.get("data");
        String rtspUrl = (String) msg.get("url");
        return rtspUrl;
    }

    public static Integer getCameraStatus(String cameraIndexCode){
        try {
            String statusJson = GetCameraPreviewURLUtil.GetCameraStatus(cameraIndexCode);
            JSONObject jsonObject = JSONObject.parseObject(statusJson);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray list = data.getJSONArray("list");
            return list.getJSONObject(0).getInteger("online");
        }catch (Exception e){
            log.error("===========================  获取设备状态异常:{}  ===========================",e.getMessage());
            e.printStackTrace();
            return CameraDetectStatus.NOT_DETECTED.getCode();
        }
    }

    public static Map<String,Object> getCameraStatusMap(List<String> cameraIndexCode){

        HashMap<String, Object> res = new HashMap<>(16);

        String urlJson = GetCameraStatusList(cameraIndexCode);
        JSONObject jsonObject = JSONObject.parseObject(urlJson);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        for (int i = 0; i < list.size(); i++) {
            JSONObject item = list.getJSONObject(i);
            String indexCode = item.getString("indexCode");
            try {
                Integer online = item.getInteger("online");
                res.put(indexCode,online);
            }catch (Exception e){
                log.error("===========================  获取设备状态异常:{}  ===========================",e.getMessage());
                e.printStackTrace();
                res.put(indexCode,CameraDetectStatus.NOT_DETECTED.getCode());
            }

        }

        return res;
    }

    /**
     * 获取摄像头取流地址
     * @param cameraIndexCode 摄像头唯一标识
     * @param streamType 0 主码流   1 子码流
     * @return 取流地址
     */
    private static String GetCameraPreviewURL(String cameraIndexCode,Integer streamType) {

        // 设置平台参数 平台的ip端口
        ArtemisConfig.host = "10.168.192.31:443";
        // 设置平台参数 密钥appkey
        ArtemisConfig.appKey = "28292616";
        // 设置平台参数 密钥appSecret
        ArtemisConfig.appSecret = "gokGfaXduVFNHPvEAg6C";

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */

        final String previewURLsApi = ARTEMIS_PATH + "/api/vnsc/mls/v1/preview/openApi/getPreviewParam";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                log.info("=========================== 访问现场地址  ===========================");
                log.info("=========================== https://" + previewURLsApi +" ===========================");
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCode", cameraIndexCode);

        jsonBody.put("netZoneCode", "0");
        jsonBody.put("transmode", 1);

        jsonBody.put("streamType", streamType);
        jsonBody.put("protocol", "rtsp");
        jsonBody.put("expireTime", -1);
        jsonBody.put("expand", "streamform=rtp");
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        log.info("===========================  调用摄像头平台入参：{}  ===========================",body);
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
        return result;
    }

    /**
     * 获取摄像头取流地址
     * @param cameraIndexCode 摄像头唯一标识
     * @return 摄像头状态
     */
    private static String GetCameraStatus(String cameraIndexCode) {
        // 设置平台参数 平台的ip端口
        ArtemisConfig.host = "10.168.192.31:443";
        // 设置平台参数 密钥appkey
        ArtemisConfig.appKey = "28292616";
        // 设置平台参数 密钥appSecret
        ArtemisConfig.appSecret = "gokGfaXduVFNHPvEAg6C";

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */

        final String previewURLsApi = ARTEMIS_PATH +"/api/nms/v1/online/camera/get";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                log.info("=========================== 访问现场地址  ===========================");
                log.info("=========================== https://" + previewURLsApi +" ===========================");
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";
        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        String[] strings = new String[1];
        strings[0]=cameraIndexCode;
        jsonBody.put("indexCodes",strings );

        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        log.info("===========================  获取摄像头状态接口入参：{}  ===========================",body);
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
        log.info("===========================  摄像头状态接口返回：{}   ===========================",result);
        return result;

    }


    private static String GetCameraStatusList(List<String> cameraIndexCodes) {
        // 设置平台参数 平台的ip端口
        ArtemisConfig.host = "10.168.192.31:443";
        // 设置平台参数 密钥appkey
        ArtemisConfig.appKey = "28292616";
        // 设置平台参数 密钥appSecret
        ArtemisConfig.appSecret = "gokGfaXduVFNHPvEAg6C";

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */

        final String previewURLsApi = ARTEMIS_PATH +"/api/nms/v1/online/camera/get";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                log.info("=========================== 访问现场地址  ===========================");
                log.info("=========================== https://" + previewURLsApi +" ===========================");
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";
        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCodes",cameraIndexCodes );

        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        log.info("===========================  获取摄像头状态接口入参：{}  ===========================",body);
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
        log.info("===========================  摄像头状态接口返回：{}   ===========================",result);
        return result;

    }


    public static void main(String[] args) {
        String json ="{\"code\":\"0\",\"msg\":\"success\",\"data\":{\"url\":\"rtsp://10.168.192.38:554/openUrl/GtXyT8Q\"}}";

        JSONObject jsonObject = JSONObject.parseObject(json);
        Object msg1 = jsonObject.get("data");
        System.out.println(msg1.toString());
        JSONObject msg = (JSONObject) jsonObject.get("data");
        String rtspUrl = (String) msg.get("url");
        System.out.println(rtspUrl);
    }
}